<?php
/**
 * Framework_User_Peopletab
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @filesource
 */

// Define the status owner relationship
define('USER_FRIENDS_WITH_OWNER',3);
define('USER_WATCHED_BY_OWNER',2);
define('USER_WATCHING_OWNER',1);
define('USER_NO_RELATION_OWNER',0);

// Define the status of the user
define('OWNER_USER_STATUS',2);
define('NORMAL_USER_STATUS',1);
define('ANONYMOUS_USER_STATUS',0);

/**
 * Framework_User_Peopletab
 *
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @package     Framework
 * @see         Framework_User
 */
class Framework_User_Peopletab extends Framework_User
{
	/**
	 * $isValid
	 *
	 * @access      public
	 * @var         boolean      $isValid
	 */
	public $isValid;

	/**
	 * __construct
	 *
	 *
	 * @access      public
	 * @param		integer		$userID
	 * @param		string		$peopletabLogin		peopletabLogin string in format user@domain.com
	 * @return      void
	 * @throws      Framework_Exception
	 */
	public function __construct($userID = null, $peopletabLogin = null)
	{

		if (!is_null($peopletabLogin)) {
			parent::__construct(-1);
			if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $peopletabLogin)) {
				$peopletabLogin = explode('@', $peopletabLogin);
				$queryArray = array(
				'sqlKey' => array(
				'select' => '*',
				'where'  => 'userName = ? AND siteDomain = ?',
				'get'    => 'row'
					),
				'params' =>  array($peopletabLogin[0], $peopletabLogin[1])

				);
				$siteUserTable = Framework_DB::tableFactory('SiteUser');

				$result = $siteUserTable->select($queryArray);
				if (!PEAR::isError($result) && count($result)) {
					$this->data = $result;
					$this->isValid = true;
				} else {
					$this->isValid = false;
				}
			} else {

				$this->isValid = false;
			}
				
			$owner = Framework_Owner::singleton();
				

			if ($owner->isValid){
				$this->friendStatus = $owner->friendStatus($this);
			}
				
			
				
		} else {
			try{
				parent::__construct($userID);
			} catch (Exception $e) {
				$this->isValid = false;
			}
		}

		if (!$peopletabLogin) {
			if (!isset($this->isValid)) {
				$this->isValid = true;
			}
	
			// Should be a function
			$owner = Framework_Owner::singleton();
	
	
			if ($owner->isValid){
				$this->friendStatus = $owner->friendStatus($this);
			}
	
			$session = Framework_Session::singleton();
	
			// Another function
			switch(true) {
				case ($owner->isValid && ($owner->idOwner == $this->idOwner)) :
					$this->userStatus = OWNER_USER_STATUS;
	
					break;
	
				case (!$this->isDefault()) :
					// currently browsing
	
					if ($owner->isValid) {
							
						$session->updateField('curBrowse',$owner->idSiteUser);
						$session->updateField('idSiteUser',$this->idSiteUser);
						$owner->updateVisitHistory($this->idSiteUser);
					}
					$this->userStatus = NORMAL_USER_STATUS;
					break;
	
				default:
					$this->userStatus = ANONYMOUS_USER_STATUS;
					if ($owner->isValid) {
						$session->updateField('idSiteUser',(string)Framework::$site->config->user->defaultUser);
					}
					break;
			}
		}
	}

	/**
	 * getUserStatus
	 *
	 *
	 * @access      public
	 * @return      integer		User Status
	 */
	public function getUserStatus()
	{
		return $this->userStatus;
	}

}